/*
 * Copyright (c) 2011. This file is public under GPLv3 license.
 * For more information about license please refer to http://www.gnu.org/licenses/gpl.html
 */

package com.freetools.watchdog.commands;

import com.freetools.watchdog.exceptions.ParametersErrorException;
import com.freetools.watchdog.model.CommandName;
import com.freetools.watchdog.model.ICommand;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Date: Feb 15, 2011
 *
 * @author Michael Morozov
 */
public abstract class Command implements ICommand
{
    public static final String[] EMPTY_NAME_COMMAND_NAME = new String[]{"<untitled command>"};
    private List<String> parameters = new ArrayList<String>();
    private static final Logger log = Logger.getLogger(Command.class);

    public List<String> getParameters()
    {
        return parameters;
    }

    public void checkParameters(int minParamSize, int maxParamSize) throws ParametersErrorException
    {
        List<String> parameters = getParameters();
        if (!(parameters.size() >= minParamSize && parameters.size() <= maxParamSize &&
                parameters.get(0) != null &&
                parameters.get(0).length() > 0))
        {
            String message = "Quantity of required parameters is not appreciated to this command";
            log.error(message);
            throw new ParametersErrorException(message);
        }
    }

    public void checkParameters() throws ParametersErrorException
    {
        checkParameters(1, 1);
    }

    public String[] getName()
    {
        CommandName commandName = getClass().getAnnotation(CommandName.class);
        if (commandName != null)
        {
            return commandName.value();
        } else
        {
            return EMPTY_NAME_COMMAND_NAME;
        }
    }

    public boolean isMultipleParametrized()
    {
        return false;
    }
}
